<template>
    <!-- 聊天框 -->
    <div
        class="chat-info-box"
        id="chat-info-box"
        ref="chatbox"
        @mousewheel="scrollChat"
    >
        <p v-if="nomoreHistory" class="load-more-btn">已无历史消息</p>
        <p
            v-show="chatInfoList.length > 0 && !nomoreHistory"
            class="load-more-btn"
            @click="loadMoreBtn"
        >
            点击加载更多
        </p>
        <div
            class="chat-info-list"
            v-for="(item, index) in chatInfoList"
            :key="index"
        >
            <div class="chat-time">{{ item.timestamp }}</div>
            <div v-if="item.about == 1" class="msg-dialogue msg-left">
                <div class="img-chat">
                    <img :src="imgOther" alt="" />
                </div>
                <div class="msg-chat-info">
                    <p>
                        <span v-if="item.messageType == 'text'">{{
                            item.message
                        }}</span>
                        <img
                            v-else-if="item.messageType == 'img'"
                            :src="item.imgUrl"
                            alt=""
                        />
                    </p>
                </div>
            </div>
            <div v-else class="msg-dialogue msg-right">
                <div class="img-chat">
                    <img :src="imgMy" alt="" />
                </div>
                <div class="msg-chat-info">
                    <p>
                        <span v-if="item.messageType == 'text'">{{
                            item.message
                        }}</span>
                        <img
                            v-else-if="item.messageType == 'img'"
                            :src="item.imgUrl"
                            alt=""
                        />
                    </p>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
export default {
    name: 'ChatList',
    props: {
        chatInfoList: {
            type: Array,
            default: () => {
                return [
                    {
                        about: 1, // 1 表示别人发生的消息, 0表示自己发生的消息
                        message: '',
                        messageType: '', // 发送消息的类型, text 表示文字  img 表示图片
                        imgUrl: '', // 图片的路径
                        timestamp: '' // 发送消息的时间
                    }
                ];
            }
        },
        nomoreHistory: {
            type: Boolean,
            default: () => {
                return false;
            }
        },
        scrollToHeight: {
            type: Boolean,
            default: () => {
                return false;
            }
        }
    },
    data() {
        return {
            imgOther: require('@/assets/image/my.png'),
            imgMy: require('@/assets/image/my.png')
        };
    },
    created() {},
    mounted() {
        this.$refs['chatbox'].addEventListener('scroll', this.scrollChat);
    },
    watch: {
        chatInfoList: function(newVal, oldVal) {
            this.$nextTick(() => {
                if (this.scrollToHeight) {
                    this.$refs['chatbox'].scrollTop = this.$refs[
                        'chatbox'
                    ].scrollHeight;
                } else {
                    // this.$refs['chatbox'].scrollTop = document.getElementById('chat-info-box').offsetHeight;
                    if (this.nomoreHistory) {
                        this.$refs['chatbox'].scrollTop = 0;
                        return false;
                    }
                    this.$refs['chatbox'].scrollTop = 1200;
                }
            });
        },
        scrollToHeight: function(newVal, old) {
            // console.log(newVal, 'hei');
        }
    },
    methods: {
        scrollChat() {
            let scrollTop = this.$refs['chatbox'].scrollTop;
            let scrollHeight = this.$refs['chatbox'].scrollHeight;
        },
        loadMoreBtn() {
            this.$emit('loading-more');
        }
    }
};
</script>

<style lang="less" scoped>
.chat-info {
    &-box {
        flex: 1;
        overflow-y: auto;
        box-sizing: border-box;
        padding: 1.5rem 0;
        .load-more-btn {
            text-align: center;
            font-size: 1rem;
            color: #ccc;
            cursor: pointer;
            padding-bottom: 1rem;
        }
    }

    &-list {
        text-align: center;
        margin-bottom: 1rem;
        .msg-dialogue {
            padding-left: 2.5rem;
            padding-top: 1rem;
            display: flex;
            align-items: center;
            .img-chat {
                margin-right: 1.25rem;
                img {
                    width: 3.87rem;
                    height: 3.87rem;
                    border-radius: 0.5rem;
                }
            }
            .msg-chat-info {
                p {
                    width: 28.16rem;
                    min-height: 6.38rem;
                    box-shadow: 0rem 0rem 0.5rem 0rem rgba(0, 0, 0, 0.12);
                    position: relative;
                    padding: 2rem 1.58rem 1.58rem;
                    box-sizing: border-box;
                    text-align: left;
                    &::before {
                        content: '';
                        position: absolute;
                        width: 1rem;
                        height: 1rem;
                        background-color: #fff;
                        box-shadow: 0.1rem 0rem 0.1rem 0rem rgba(0, 0, 0, 0.12);
                        left: -0.5rem;
                        top: 50%;
                        transform: translateY(-50%) rotate(135deg);
                    }
                    img {
                        width: 3.87rem;
                        height: 3.87rem;
                        border-radius: 0.5rem;
                    }
                }
            }
        }
        .msg-right {
            flex-direction: row-reverse;
            .img-chat {
                img {
                    margin-left: 1.25rem;
                    margin-right: 1.5rem;
                }
            }
            .msg-chat-info {
                p {
                    color: #fff;
                    background-color: #2364d9;
                    &::before {
                        background-color: #2364d9;
                        right: -0.5rem;
                        left: auto;
                        box-shadow: none;
                    }
                }
            }
        }
    }
}
</style>
